#include<iostream>
#include<cstring> 
#include<algorithm>
using namespace std;
const int N=110;
int g[N][N];
bool st[N];
int dist[N];
int n,m;

int dij(){
	memset(dist,0x3f,sizeof dist);
	memset(st,false,sizeof st);
	dist[1]=0;
	for(int i=0;i<n-1;i++){
		int t=-1;
		for(int j=1;j<=n;j++){
			if(!st[j]&&(t==-1||dist[t]>dist[j])){
				t=j;
			}
		}
		st[t]=true;
		for(int j=1;j<=n;j++){
			dist[j]=min(dist[j],dist[t]+g[t][j]);
		}
				
	}
	
	return dist[n];	
	
}


int main(){
   while(cin>>n>>m,n&&m){
    memset(g,0x3f,sizeof g);
    for(int i=0;i<m;i++){
    	int a,b,c;
    	cin>>a>>b>>c;
    	g[b][a]=g[a][b]=min(c,g[a][b]);
	}
	cout<<dij()<<endl;
 
   }
  
   
	
	return 0;
} 
